/**   
 * @Title: IPEdit.java 
 * @Package android.framework.widget.exittext 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-8-20 下午4:21:39 
 * @version V1.0   
 */
package android.framework.widget.exittext;

import android.framework.R;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 * @ClassName: IPEdit
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-8-20 下午4:21:39
 * 
 */
public class IPEdit extends LinearLayout {

	public IPEdit(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initEdit(context);
	}

	private void initEdit(Context context) {
		LinearLayout parent = (LinearLayout) LayoutInflater.from(context)
				.inflate(R.layout.ip_edit, null);
		for (int i = 0; i < parent.getChildCount(); i++) {
			if (parent.getChildAt(i) instanceof EditText) {
				((EditText) parent.getChildAt(i))
						.addTextChangedListener(new IPTextWatcher(i, parent));
			}
		}
		addView(parent, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
	}

	class IPOnEditorActionListener implements OnEditorActionListener {
		int index;
		ViewGroup parent;

		public IPOnEditorActionListener(int index, ViewGroup parent) {
			this.index = index;
			this.parent = parent;
		}

		@Override
		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
			// TODO Auto-generated method stub
			if (actionId == EditorInfo.IME_ACTION_DONE) {

			}
			return false;
		}

	}

	class IPTextWatcher implements TextWatcher {

		int index;
		ViewGroup parent;

		public IPTextWatcher(int index, ViewGroup parent) {
			this.index = index;
			this.parent = parent;
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			String text = s.toString();
			if (text.length() == 3) {
				if (index < 6) {
					parent.getChildAt(index + 2).requestFocus();
				}
			} else if (text.length() == 0) {
				if (index > 0) {
					parent.getChildAt(index - 2).requestFocus();
				}
			}
		}
	};
}
